package com.ruoyi.ruoyiqywechat.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.qy.domain.SystemConfig;
import com.ruoyi.common.qy.vo.MembershipPlan;
import com.ruoyi.ruoyiqywechat.service.SystemConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * 配置信息
 */
@RestController
@RequestMapping("sysConfig")
public class SysConfigController {

    @Autowired
    private SystemConfigService systemConfigService;
    /**
     * 会员配置
     * @return
     */
    @GetMapping("querySystemConfigMember")
    public R<ArrayList<MembershipPlan>> querySystemConfigMember(){
        String valueByKey = systemConfigService.getValueByKey("membership_plans_form");
        JSONObject jsonObject = JSONObject.parseObject(valueByKey);
        JSONArray jsonArray = jsonObject.getJSONArray("membership_plans");
        ArrayList<MembershipPlan> membershipPlanArrayList = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject arrayJSONObject = jsonArray.getJSONObject(i);
            MembershipPlan membershipPlan = JSONObject.parseObject(arrayJSONObject.toJSONString(), MembershipPlan.class);
            membershipPlanArrayList.add(membershipPlan);
        }
        System.out.println("当前配置信息："+valueByKey);
        return R.ok(membershipPlanArrayList);
    }
    /**
     * 显示隐藏视频
     * @return
     */
    @GetMapping("queryConfigHidden")
    public R<String> queryConfigHidden(){
        String valueByKey = systemConfigService.getValueByKey("queryConfigHidden");
        return R.ok(valueByKey);
    }

    /**
     * 显示隐藏首页内容
     * @return
     */
    @GetMapping("queryIndexConfigHidden")
    public R<String> queryIndexConfigHidden(@RequestParam("key") String key){
        String valueByKey = systemConfigService.getValueByKey(key);
        return R.ok(valueByKey);
    }
}
